﻿using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using TMV.Models;
using TMV.Models.Input;

namespace TMV.Common
{
    public class ApplicationInitializer :
        DropCreateDatabaseAlways<ApplicationDbContext>
        //DropCreateDatabaseIfModelChanges<ApplicationDbContext>
    {
        protected override void Seed(ApplicationDbContext context)
        {
            InitializeIdentityForEF(context);
            base.Seed(context);
        }

        private void InitializeIdentityForEF(ApplicationDbContext context)
        {
            var roleManager = new ApplicationRoleManager(new RoleStore<ApplicationRole>(context));
            var userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(context));

            AddAdministrator(context, roleManager, userManager);
        }

        private void AddAdministrator(ApplicationDbContext context, ApplicationRoleManager roleManager, ApplicationUserManager userManager)
        {
            //const string userName = "tig1hc";
            const string email = "venturousman@yahoo.com";
            const string password = "tmv@2016";
            const string roleName = "Administrator";

            // Create Role if it does not exist
            var role = roleManager.FindByName(roleName);
            if (role == null)
            {
                role = new ApplicationRole(roleName);
                var roleresult = roleManager.Create(role);
            }

            // Create User if it does not exist
            var user = userManager.FindByName(email);
            if (user == null)
            {
                user = new ApplicationUser
                {
                    UserName = email,                    
                    Email = email,
                    //DisplayName = "Nguyen Hoang Tin (RBVH/ETI1)",
                    //Avatar = string.Format("https://rb-owa.apac.bosch.com/ews/exchange.asmx/s/GetUserPhoto?email={0}&size=HR240x240", name),
                    //NTID = "tig1hc"
                };
                var result = userManager.Create(user, password);
                result = userManager.SetLockoutEnabled(user.Id, false);
            }

            // Add user admin to Role if not already added
            var rolesForUser = userManager.GetRoles(user.Id);
            if (!rolesForUser.Contains(role.Name))
            {
                var result = userManager.AddToRole(user.Id, role.Name);
            }

            // Create Permissions if they do not exist
            var permissions = new List<Permission> {
                // Role
                new Permission { Code = Rights.View + Modules.Role, Description = "View Role", Name = "View Role" },
                new Permission { Code = Rights.Create + Modules.Role, Description = "Create Role", Name = "Create Role" },
                new Permission { Code = Rights.Modify + Modules.Role, Description = "Modify Role", Name = "Modify Role" },
                new Permission { Code = Rights.Delete + Modules.Role, Description = "Delete Role", Name = "Delete Role" },                
            };

            foreach (var permission in permissions)
            {
                var permissionInDb = context.Permissions.FirstOrDefault(x => x.Code == permission.Code);
                if (permissionInDb == null)
                {
                    permissionInDb = context.Permissions.Add(permission);
                }
                // Add permissions to Role Admin if not already added
                role.Permissions.Add(permissionInDb);
            }
            context.SaveChanges();
        }
    }
}